package com.itboye.jinpuyiyuan.activity.home.register;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.activity.login.LoginOrRegisterActivity;
import com.itboye.jinpuyiyuan.activity.login.RegistActivity;
import com.itboye.jinpuyiyuan.adapter.BranchListAdapter;
import com.itboye.jinpuyiyuan.adapter.FindDoctorArrayAdapter;
import com.itboye.jinpuyiyuan.adapter.LeftAdapter;
import com.itboye.jinpuyiyuan.adapter.RightAdapter;
import com.itboye.jinpuyiyuan.base.BaseActivity;
import com.itboye.jinpuyiyuan.bean.BranchBean;
import com.itboye.jinpuyiyuan.bean.BranchDepartmentBean;
import com.itboye.jinpuyiyuan.bean.RegisterDoctorDetail;
import com.itboye.jinpuyiyuan.bean.RegisterHospitalBean;
import com.itboye.jinpuyiyuan.presenter.BranchAddressPresenter;
import com.itboye.jinpuyiyuan.utils.sputil.Const;
import com.itboye.jinpuyiyuan.utils.sputil.SPUtils;
import com.itboye.jinpuyiyuan.utils.volley.ResultEntity;
import com.itboye.jinpuyiyuan.widget.FilterUtil;
import com.itboye.jinpuyiyuan.widget.loadingutil.MAlert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by Administrator on 2017/12/9 0009.
 *
 */

public class RegisterActivity extends BaseActivity implements AdapterView.OnItemClickListener, Observer {
    //title
    private ImageView leftImg,rightImg;
    private TextView leftTv,tvTitle;
    private RelativeLayout rlLeft;


    private BranchAddressPresenter presenter,presenter1;
    private BroadcastReceiver receiver;
    private HashMap<String,Integer> map=new HashMap<>();//string分院名称，integer 分院id
    private List<BranchDepartmentBean> departmentList;//存放科室
    private List<BranchDepartmentBean.DepartmentChild> childList;//存放科室子科室（如儿科的外科和内科）
    private LinearLayout branchswitch,top_line,sortLl,sortlist_Ll,branchList_Ll;
    private ListView leftList,rightList,switchBranchList,sortList;


    private ArrayList<String> arrayList,sortArray;

    private LeftAdapter leftAdapter;
    private RightAdapter rightAdapter;

    private FindDoctorArrayAdapter branchSwitchAdapter//切换分院
            ,sortAdapter;//切换分院

    private TextView branchName,sortTv;
    private PopupWindow switchBranchPop;
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_register_hospital);

        leftTv.setVisibility(View.GONE);
        rightImg.setVisibility(View.GONE);
        tvTitle.setText("挂号");


        //接受登录和退出登录的广播
        receiver=new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals(FilterUtil.LOGIN)){
                  //数据刷新

                }else  if (intent.getAction().equals(FilterUtil.EXITLOGIN)){
                    //登录
                 startActivity(new Intent(RegisterActivity.this, LoginOrRegisterActivity.class));

                }

            }
        };
       registerReceiver(receiver,new IntentFilter(FilterUtil.LOGIN));
       registerReceiver(receiver,new IntentFilter(FilterUtil.EXITLOGIN));
        presenter=new BranchAddressPresenter(this);
        presenter1=new BranchAddressPresenter(this);

        presenter.queryBranch(0,0);//分院查询

        arrayList=new ArrayList<>();
        sortArray=new ArrayList<>();
        departmentList=new ArrayList<>();//存放科室的list
        childList=new ArrayList<>();

        sortArray.add("全部");
        sortArray.add("门诊");
        sortArray.add("住院");
        sortArray.add("体检");

        branchSwitchAdapter=new FindDoctorArrayAdapter(this,arrayList);
        sortAdapter=new FindDoctorArrayAdapter(this,sortArray);
        switchBranchList.setAdapter(branchSwitchAdapter);
        sortList.setAdapter(sortAdapter);

        sortlist_Ll.setVisibility(View.GONE);
        branchList_Ll.setVisibility(View.GONE);


        switchBranchList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                branchName.setText( arrayList.get(position));
                branchList_Ll.setVisibility(View.GONE);
                presenter1.queryDepartment(map.get(arrayList.get(position)));

            }
        });
       sortList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               sortTv.setText("种类:  "+sortArray.get(position));
               sortTv.setTextColor(getResources().getColor(R.color.black));
               sortTv.setCompoundDrawablesWithIntrinsicBounds(null,null,getResources().getDrawable(R.drawable.guahao_jiantou),null);
               sortlist_Ll.setVisibility(View.GONE);
           }
       });



        rightAdapter=new RightAdapter(this,childList);
        rightList.setAdapter(rightAdapter);
        leftList.setOnItemClickListener(this);
        rightList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(RegisterActivity.this, RegisterDetailActivity.class);
                intent.putExtra("title",childList.get(position).getName());
                startActivity(intent);
            }
        });

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.rlLeft:
            case R.id.leftImg:
                this.finish();
                break;
            case R.id.branchswitch:
            case R.id.branchswitchImg:
            case R.id.branchswitchTv:
                branchList_Ll.setVisibility(View.VISIBLE);
                sortlist_Ll.setVisibility(View.GONE);
                break;
            case R.id.sortlist_Ll:
                sortlist_Ll.setVisibility(View.GONE);
                sortTv.setTextColor(getResources().getColor(R.color.black));
                sortTv.setCompoundDrawablesWithIntrinsicBounds(null,null,getResources().getDrawable(R.drawable.guahao_jiantou),null);
                break;
            case R.id.branchList_Ll:
                branchList_Ll.setVisibility(View.GONE);
                break;
            case R.id.sortLl:
            case R.id.sortTv:
                sortTv.setTextColor(getResources().getColor(R.color.title_blue));
                sortTv.setCompoundDrawablesWithIntrinsicBounds(null,null,getResources().getDrawable(R.drawable.xuanzhongsort),null);
                sortlist_Ll.setVisibility(View.VISIBLE);
                branchList_Ll.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

//        Log.d("======",list.get(position).getList().toString());
               leftAdapter.cancleAllItemState(position);
               leftAdapter.notifyDataSetChanged();


//                Log.d("======",list.get(position).getList().toString());
                rightAdapter.addData(departmentList.get(position).getChild());

    }

    @Override
    public void update(Observable o, Object arg) {

        ResultEntity entity = handlerError(arg);
        Log.d("======entity",entity.toString());
        if (entity != null) {
            if (entity.getEventType() == BranchAddressPresenter.queryAddress_success) {
//                MAlert.alert("请求成功");

                BranchBean bean = (BranchBean) entity.getData();
                Log.d("======up",bean.toString());
                for (int i=0;i<bean.getList().size();i++){
                    arrayList.add(bean.getList().get(i).getName());
                    map.put(bean.getList().get(i).getName(),bean.getList().get(i).getId());
                }

                branchName.setText( arrayList.get(0));
//        branchList_Ll.setVisibility(View.GONE);
                presenter1.queryDepartment(map.get(arrayList.get(0)));


            } else if (entity.getEventType() == BranchAddressPresenter.queryAddress_fail) {
//                MAlert.alert(entity.getMsg(), Gravity.TOP);
            }else if (entity.getEventType()== BranchAddressPresenter.By_HospSect_query_success){
//                MAlert.alert("科室查询成功");
                if (departmentList.size()!=0){
                    departmentList.clear();
                }
                 departmentList.addAll((List<BranchDepartmentBean>) entity.getData()) ;
                leftAdapter=new LeftAdapter(this,departmentList);
                leftList.setAdapter(leftAdapter);
                childList.clear();
                childList.addAll(departmentList.get(0).getChild());
                rightAdapter.addData(departmentList.get(0).getChild());//加载默认第一个选中的数据
            }else if (entity.getEventType() == BranchAddressPresenter.By_HospSect_query_fail){
//                MAlert.alert("科室查询失败");

            }


        }
    }
}
